﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Data;
using System.Globalization;


namespace Cnblogs
{
    public class CreatedAtConverter : IValueConverter
    {
        // Methods
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || string.IsNullOrEmpty(value.ToString())) return "";
            try
            {
                //根据"Thu Jun 16 16:14:06 +0800 2011" 计算出区域性本地时间，update by linan20110617
                CultureInfo provider = CultureInfo.InvariantCulture;
                DateTime dt = DateTime.ParseExact(value.ToString(), "ddd MMM dd HH':'mm':'ss zzzz yyyy", provider);

                TimeSpan timeSpan = DateTime.Now - dt;

                string strDateNoTime = "";
                switch (dt.Month)
                {
                    case 1:
                        strDateNoTime = "Jan";
                        break;
                    case 2:
                        strDateNoTime = "Feb";
                        break;
                    case 3:
                        strDateNoTime = "Mar";
                        break;
                    case 4:
                        strDateNoTime = "Apr";
                        break;
                    case 5:
                        strDateNoTime = "May";
                        break;
                    case 6:
                        strDateNoTime = "Jun";
                        break;
                    case 7:
                        strDateNoTime = "Jul";
                        break;
                    case 8:
                        strDateNoTime = "Aug";
                        break;
                    case 9:
                        strDateNoTime = "Sep";
                        break;
                    case 10:
                        strDateNoTime = "Oct";
                        break;
                    case 11:
                        strDateNoTime = "Nov";
                        break;
                    case 12:
                        strDateNoTime = "Dec";
                        break;
                }
                strDateNoTime += " " + dt.Day.ToString();

                string minute = dt.Minute < 10 ? string.Format("0{0}", dt.Minute.ToString()) : dt.Minute.ToString();
                return strDateNoTime + " " + dt.Hour + ":" + minute;

            }
            catch (Exception e)
            {
                return value.ToString();
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }


    }
}
